package com.das.mechanic_base.adapter.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.main.CarPoolBean;
import com.das.mechanic_base.utils.X3StringUtils;

import java.util.ArrayList;
import java.util.List;

public class X3CarPoolAdapter extends RecyclerView.Adapter<X3CarPoolAdapter.CarPoolViewHolder> {

    private List<CarPoolBean> mList;

    private Context mContext;

    private boolean isName;

    public X3CarPoolAdapter(Context mContext) {
        this.mContext = mContext;
        this.mList = new ArrayList<>();

    }
    public void setData(List<CarPoolBean> mList){
        this.mList = mList;
        notifyDataSetChanged();
    }



    @NonNull
    @Override
    public CarPoolViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CarPoolViewHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_item_car_pool,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull CarPoolViewHolder holder, int position) {
        if(mList.get(position) != null){
            if(!X3StringUtils.isEmpty(mList.get(position).label)){
                holder.tv_carnum.setText(mList.get(position).label + "");
            }
            Glide.with(mContext).load(X3StringUtils.getImageUrl(mList.get(position).kwParam.carBrandImgUrl)).apply(
                    new RequestOptions().placeholder(R.mipmap.x3_car_icon_no).skipMemoryCache(false).dontAnimate()
            ).into(holder.iv_icon);
            if(!X3StringUtils.isEmpty(mList.get(position).kwParam.carBrandName)){
                holder.tv_brandname.setText(mList.get(position).kwParam.carBrandName + "");
            }
            if(!X3StringUtils.isEmpty(mList.get(position).kwParam.carStyleName)){
                holder.tv_brandname.setText(mList.get(position).kwParam.carStyleName + "");
            }
        }

        holder.itemView.setOnClickListener(view -> {
            if(iOnItemClick != null) {
                iOnItemClick.iOnItemClick(mList.get(position));
            }
        });
    }

    public boolean isGroupHead(int position) {
        if (position >= mList.size()) {
            return false;
        }
        if (position == 0) {
            return true;
        } else {
            String currentGroup = getGroupName(position);
            String preGroup = getGroupName(position - 1);
            return !currentGroup.equals(preGroup);
        }
    }

    /**
     * 获取组名
     */
    public String getGroupName(int position){

        String sortLetters = mList.get(position).sortLetters;

        return "🔥".equals(sortLetters) ? mContext.getString(R.string.x3_common_brand) : sortLetters;
    }

    IOnItemClick iOnItemClick;

    public void setiOnItemClick(IOnItemClick iOnItemClick) {
        this.iOnItemClick = iOnItemClick;
    }

    public interface IOnItemClick {
        void iOnItemClick(CarPoolBean name);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class CarPoolViewHolder extends RecyclerView.ViewHolder {
        TextView tv_name;
        ImageView iv_icon;
        TextView tv_num;
        TextView tv_carnum;
        TextView tv_brandname;
        TextView tv_stylename;
        public CarPoolViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_name = itemView.findViewById(R.id.tv_name);
            iv_icon = itemView.findViewById(R.id.iv_icon);
            tv_num = itemView.findViewById(R.id.tv_num);
            tv_carnum = itemView.findViewById(R.id.tv_carnum);
            tv_brandname = itemView.findViewById(R.id.tv_brandname);
            tv_stylename = itemView.findViewById(R.id.tv_stylename);
        }
    }
}

